package test;

/**
 * @Author Stringzhua
 * @Date 2020/11/7 23:11
 * description:
 */
public class Demo02 {
    public static void main(String[] args) {
        String str = "14444112442aa44442244311222222244445444243334444ccc4444";
        findChar(str);
    }
    public static void findChar(String str) {
        //判空
        if (str == null || str.isEmpty()) {
            System.out.println("输入字符串为空，请输入有效字符串。");
            return;
        }
        //重复出现次数的最大值
        int max = 0;
        //出现次数最多的字符
        char maxChar = '\0';
        //计数器
        int count = 1;
        //当前正在统计的字符
        char charNow = str.charAt(0);
        //遍历输入的字符串~
        for (int i = 1; i < str.length(); i++) {
            //当前字符和正在统计的字符相同说明是同一组连续重复字符
            if (str.charAt(i) == charNow) {
                //计数器+1
                count++;
            } else {//不同
                if (count > max) {//更新计数器和最大字符
                    max = count;
                    maxChar = charNow;
                }
                //重置计数器
                count = 1;
                //正在统计的字符修改为当前字符
                charNow = str.charAt(i);
            }
        }

        // 处理最后一组连续重复字符的情况
        if (count > max) {
            max = count;
            maxChar = charNow;
        }

        System.out.println(maxChar + "," + max);
    }
}